package com.ruoyi.house_sale.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.house_sale.domain.CustEvent;
import com.ruoyi.house_sale.domain.CustomerInfo;
import com.ruoyi.house_sale.domain.EventType;
import com.ruoyi.house_sale.domain.Houseinfo;
import com.ruoyi.house_sale.service.ICustEventService;
import com.ruoyi.house_sale.service.ICustomerInfoService;
import com.ruoyi.house_sale.service.IEventTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/house_sale/customer")
public class CustomerInfoController  extends BaseController {

    @Autowired
    private ICustomerInfoService customerInfoService;
    @Autowired
    private ICustEventService custEventService;

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerInfo customerInfo)
    {
        startPage();
        List<CustomerInfo> list = customerInfoService.selectCustomerInfoList(customerInfo);
        return getDataTable(list);
    }
    /**
     * 查询业务员
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:getUser')")
    @GetMapping("getUser/{deptid}")
    public AjaxResult getUser(@PathVariable("deptid") Long deptid)
    {
        return AjaxResult.success(customerInfoService.selectUser(deptid));
    }
    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:export')")
    @Log(title = "客户台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,CustomerInfo customerInfo)
    {
        List<CustomerInfo> list =customerInfoService.selectCustomerInfoList(customerInfo);
        ExcelUtil<CustomerInfo> util = new ExcelUtil<CustomerInfo >(CustomerInfo.class);
        util.exportExcel(response, list, "客户列表");
    }

    /**
     * 获取客户
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:query')")
    @GetMapping(value = "/{custid}")
    public AjaxResult getInfo(@PathVariable("custid") Long custid)
    {
        return AjaxResult.success(customerInfoService.selectCustomerInfoByCustid(custid));
    }

    /**
     * 新增客户
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:add')")
    @Log(title = "客户台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerInfo customerInfo)
    {
        customerInfoService.insertCustomerInfo(customerInfo);
        CustomerInfo  customerInfo1=customerInfoService.insertCustEvent();
        CustEvent custEvent=new CustEvent();
        custEvent.setCustid(customerInfo1.getCustId());
        custEvent.setEventtime(customerInfo1.getSigndate());
        custEvent.setChance(customerInfo1.getChance());
        custEvent.setEventtypeid(customerInfo1.getEventtypeid());
        custEvent.setSalepercent(customerInfo1.getSalePercent());
        custEvent.setDescription("无");
        custEventService.insertCustEvent(custEvent);
        return toAjax(true);

    }

    /**
     * 修改客户
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:edit')")
    @Log(title = "客户台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerInfo customerInfo)
    {
        return toAjax(customerInfoService.updateCustomerInfo(customerInfo));
    }

    /**
     * 删除客户
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:remove')")
    @Log(title = "客户台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{custids}")
    public AjaxResult remove(@PathVariable Long[] custids)
    {
        return toAjax(customerInfoService.deleteCustomerInfoByCustids(custids));
    }

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:userlist')")
    @GetMapping("/userlist")
    public TableDataInfo list(SysDept dept)
    {
        List<SysDept> list = customerInfoService.selectUserList(dept);
        return getDataTable(list);
    }

    /**
     * 查询客户列表
     */
    @PreAuthorize("@ss.hasPermi('house_sale:customer:custHouse')")
    @GetMapping("/custHouse")
    public AjaxResult custHouse(Houseinfo  houseinfo)
    {
        List<Houseinfo> list=customerInfoService.selectCustHouse(houseinfo);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("houseList", list);
        return  ajax;
    }
}
